<?php

    class CicpRandomPoints implements Cicp_Handler_Interface
{
    function initGraph($x, $y, $z)
    {
        $graph = new Cicp_Graph();
        $graph->setPosition($x, $y, $z);

        $this->drawRandom($graph, 10);

        return $graph;
    }

    function menuChoice(Cicp_Graph $graph, Cicp_Graph_Node $node, Cicp_Graph_Menu $menu, $menuButtonLabel)
    {
        switch ($menuButtonLabel) {
        case '10 Redraw':
            $factor = 10;
            break;
        case '100 Redraw':
            $factor = 100;
            break;
        case '50 Redraw':
            $factor = 50;
            break;
        default:
            return $graph;
        }

        $this->clear($graph);
        $this->drawRandom($graph, $factor);
        return $graph;
    }

    function clear($graph)
    {
        $nodes = $graph->getNodes();
        foreach ($nodes as $node) {
            $graph->removeNode($node);
        }
    }

    function drawRandom($graph, $factor)
    {
        $nodes = array();
        $menu = new Cicp_Graph_Menu('default');
        $menu->addButton('10 Redraw');
        $menu->addButton('50 Redraw');
        $menu->addButton('100 Redraw');
        $menu->addButton('Delete Graph', Cicp_Graph_Menu::BUTTON_DELETE);
        $graph->addMenu($menu);
        $node = new Cicp_Graph_Node;
        $node->assignMenu($menu);
        $node->setShape('sph');
        $node->setDimensions(0., .01, 0.);
        $node->setColor(255, 255, 255);
        // set random nodes
        for ($i = 0; $i <= $factor; $i++) {
            $randomNode = $graph->registerNode(clone $node);
            $randomNode->setPosition(rand(0, $factor), rand(0, $factor), rand(0, $factor));
            $nodes[] = $randomNode;
        }

        $nodeCount = count($nodes);
        $connectorNode = new Cicp_Graph_Node;
        $connectorNode->setColor(255, 255, 255);
        $connectorNode->setHlColor(0, 0, 0);
        $connectorNode->setShape('lin');
        $connectorNode->assignMenu($menu);
        $connectedNodes = array();
        foreach ($nodes as $node) {
            $nodePosition = $node->getNodeProperty('loc1');

            // create connector node
            for ($i = 0; $i < 3; $i++) {
                $targetNode = $nodes[rand(1, $nodeCount - 1)];

                // check if this target node and actual node are not
                // the same
                if ($node->getId() == $targetNode->getId()) {
                    // --$i; // so we get another round
                    continue;
                }

                // check if nodes are not already connected
                if ((isset($connectedNodes[$node->getId()]) && $connectedNodes[$node->getId()] == $targetNode->getId()) 
                    || (isset($connectedNodes[$targetNode->getId()]) && $connectedNodes[$targetNode->getId()] == $node->getId())) {
                    continue;
                }
                else {
                    // save connection to avoid double lines
                    $connectedNodes[$targetNode->getId()] = $node->getId();
                }

                $targetNodePosition = $targetNode->getNodeProperty('loc1');
                $graph->drawLine($nodePosition, $targetNodePosition, $connectorNode);
            }
        }
    }
}